﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;

namespace SXFramWork.Core
{
    public class ServiceBuilder
    {    
        /// <summary>
        /// 构造依赖注入容器
        /// </summary>
        /// <returns></returns>
        public static IServiceProvider BuildServiceForSqlServer(string uid)
        {
            var services = new ServiceCollection();
            var dbType = ConfigHelper.app("DBConnection", "Type").To_Int();
            //代码生成、
            services.Configure<CodeGenerateOption>(options =>
            {
                options.Author = "shuyuansong";
                options.ConnectionString = ConfigHelper.app("DBConnection", "SqlServer");//数据库连接
                if (dbType == 1)
                {
                    options.ConnectionString = ConfigHelper.app("DBConnection", "MySql");//数据库连接
                }
                options.OutputPath_IRepository =Path.Combine(AppContext.BaseDirectory, "Temp","BuildCode",uid ,"Repository"); //文件存放路径
                options.OutputPath_IService = Path.Combine(AppContext.BaseDirectory, "Temp", "BuildCode",uid, "Service");
                options.OutputPath_Model = Path.Combine(AppContext.BaseDirectory, "Temp", "BuildCode",uid, "Model");
                options.OutputPath_Repository = Path.Combine(AppContext.BaseDirectory, "Temp", "BuildCode",uid, "Repository");
                options.OutputPath_Service = Path.Combine(AppContext.BaseDirectory, "Temp", "BuildCode",uid, "Service");
                options.ModelsNamespace = "SXFramWork.Model";//实体命名空间
                options.IRepositoryNamespace = "SXFramWork.Repository";//仓储接口命名空间
                options.RepositoryNamespace = "SXFramWork.Repository";//仓储命名空间
                options.IServicesNamespace = "SXFramWork.Service";//服务接口命名空间
                options.ServicesNamespace = "SXFramWork.Service";//服务命名空间
            });
            services.AddScoped<CodeGenerator>();
            return services.BuildServiceProvider(); //构建服务提供程序
        }
       
    }
}
